package com.voxel.sense.infomsg.module.task.execution.file;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.request.RequestOptions;
import com.example.com.common.utils.FileUtils;
import com.example.com.common.utils.ScreenUtils;
import com.example.com.common.utils.SizeUtils;
import com.example.com.common.view.recycler.RecyclerAdapter;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.data.bean.MultiItemBean;
import com.voxel.sense.infomsg.interfaces.listener.AddItemPortionClickListener;
import com.voxel.sense.infomsg.utils.GlideUtils;

import java.util.Collection;

import butterknife.BindView;
import cyy.example.com.facotry.constants.CommonConstants;
import cyy.example.com.facotry.constants.Constants;

/**
 * @author ChenYiYao
 * @date 2018/5/14
 * des :
 */
public class TaskFileOperationAdapter extends RecyclerAdapter<MultiItemBean<String>> {

    private AddItemPortionClickListener<MultiItemBean<String>> mItemDeletedListener;


    public TaskFileOperationAdapter() {
        super();
        mDataList.add(new MultiItemBean("", TaskFileOperationContract.ADD_TYPE));
    }

    @Override
    public void replace(Collection<MultiItemBean<String>> dataList) {
        mDataList.clear();

        if (dataList == null || dataList.size() == 0) {
            return;
        }
        mDataList.addAll(dataList);
        if (mDataList.get(0).getType() != TaskFileOperationContract.ADD_TYPE) {
            mDataList.add(0, new MultiItemBean("", TaskFileOperationContract.ADD_TYPE));
        }
        notifyDataSetChanged();

    }

    @Override
    protected int getItemViewType(int position, MultiItemBean multiItemBean) {
        if (TaskFileOperationContract.IMAGE_DESC_TYPE == multiItemBean.getType()
                || TaskFileOperationContract.DOC_DESC_TYPE == multiItemBean.getType()) {
            return R.layout.item_recycler_textview;
        } else if (TaskFileOperationContract.NET_DOC_TYPE == multiItemBean.getType()
                || TaskFileOperationContract.LOCAL_DOC_TYPE == multiItemBean.getType()) {
            return R.layout.item_recycler_task_file;
        }
        return R.layout.item_recycler_square_addpic;
    }


    @Override
    protected ViewHolder<MultiItemBean<String>> onCreateViewHolder(View root, int viewType) {
        ViewHolder holder;
        if (R.layout.item_recycler_textview == viewType) {
            holder = new HeadHolder(root);
        }else if (R.layout.item_recycler_task_file == viewType){
            holder = new FileVHolder(root);
        }else {
            holder = new VHolder(root);
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder<MultiItemBean<String>> holder, int position) {
        super.onBindViewHolder(holder, position);
        if (getItemViewType(position) == R.layout.item_recycler_square_addpic ||
                getItemViewType(position) == R.layout.item_recycler_task_file) {
            ImageView iv_delete = holder.itemView.findViewById(R.id.iv_delete);
            //删除事件
            iv_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    MultiItemBean<String> itemBean = getItems().get(position);
                    deleteItem(position);
                    if (mItemDeletedListener != null) {
                        mItemDeletedListener.onItemChildViewClick(iv_delete, position, itemBean);
                    }
                }
            });
        }
    }

    private void deleteItem(int position) {
        //删除数据，再刷新界面
        getItems().remove(position);
        notifyDataSetChanged();
    }

    public AddItemPortionClickListener<MultiItemBean<String>> getItemDeletedListener() {
        return mItemDeletedListener;
    }

    public void setItemDeletedListener(AddItemPortionClickListener<MultiItemBean<String>> itemDeletedListener) {
        mItemDeletedListener = itemDeletedListener;
    }

    public static class VHolder extends ViewHolder<MultiItemBean<String>> {
        @BindView(R.id.iv_delete)
        ImageView iv_delete;
        @BindView(R.id.iv_pic)
        ImageView iv_pic;
        ViewGroup.LayoutParams layoutParams;

        private int mImageSize;

        public VHolder(View itemView) {
            super(itemView);
            int pad = SizeUtils.dp2px(2);
            mImageSize = (ScreenUtils.getScreenWidth() - pad) / 4;
            //设置item 的大小
            layoutParams = new ViewGroup.LayoutParams(mImageSize, mImageSize);
        }

        @Override
        protected void onBind(MultiItemBean<String> multiItemBean) {
            iv_delete.setVisibility(View.VISIBLE);
            loadItemInfo(iv_pic.getContext(), multiItemBean.getType(), multiItemBean.getData());
            if (multiItemBean.getType() == TaskFileOperationContract.ADD_TYPE) {
                itemView.setLayoutParams(layoutParams);
            } else {
                itemView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            }
        }

        private void loadItemInfo(Context context, int type, String url) {
            switch (type) {
                case TaskFileOperationContract.LOCAL_DOC_TYPE:
                case TaskFileOperationContract.NET_DOC_TYPE:
                    //文档
                    String fileExtension = FileUtils.getFileExtension(url);
                    loadDocuIcon(iv_pic, fileExtension);
                    break;
                case TaskFileOperationContract.VIDEO_TYPE://视频
                    GlideUtils.GlideLoadPic(context, R.mipmap.mp4_icon, iv_pic);
                    break;
                case TaskFileOperationContract.ADD_TYPE:
                    GlideUtils.GlideLoadPic(context, R.mipmap.add_image, iv_pic);
                    iv_delete.setVisibility(View.GONE);
                    break;
                case TaskFileOperationContract.LOCAL_IMAGE_TYPE:
                    GlideUtils.GlideLoadPic(context, url, iv_pic);
                    break;
                case TaskFileOperationContract.NET_IMAGE_TYPE:
                    GlideUtils.GlideLoadPic(context, CommonConstants.FILE_TASK_URL + url, iv_pic);
                    break;
                default://默认类型图片
                    break;
            }
        }

        //初始化icon
        private void loadDocuIcon(ImageView iv_pic, String fileExtension) {
            //TODO:由于icon缺失，暂时选择word文档的icon
            Context context = iv_pic.getContext();
            if (Constants.EXTENSION_DOC.equals(fileExtension) || Constants.EXTENSION_DOCX.equals(fileExtension)) {
                //doc文档
                GlideUtils.GlideLoadPic(context, R.mipmap.word_doc_icon, iv_pic);
            } else if (Constants.EXTENSION_XLS.equals(fileExtension) || Constants.EXTENSION_XLSX.equals(fileExtension)) {
                //xls文档
                GlideUtils.GlideLoadPic(context, R.mipmap.word_doc_icon, iv_pic);
            } else if (Constants.EXTENSION_PDF.equals(fileExtension)) {
                //pdf文档
                GlideUtils.GlideLoadPic(context, R.mipmap.word_doc_icon, iv_pic);
            } else if (Constants.EXTENSION_PPT.equals(fileExtension) || Constants.EXTENSION_PPTX.equals(fileExtension)) {
                //ppt文档
                GlideUtils.GlideLoadPic(context, R.mipmap.word_doc_icon, iv_pic);
            } else {
                //默认显示文本
                GlideUtils.GlideLoadPic(context, R.mipmap.word_doc_icon, iv_pic);
            }
        }
    }

    public static class FileVHolder extends ViewHolder<MultiItemBean<String>> {
        @BindView(R.id.iv_delete)
        ImageView iv_delete;
        @BindView(R.id.iv_pic)
        ImageView iv_pic;
        @BindView(R.id.tv_desc)
        TextView tv_desc;
        ViewGroup.LayoutParams layoutParams;

        private int mImageSize;

        public FileVHolder(View itemView) {
            super(itemView);
            int pad = SizeUtils.dp2px(2);
            mImageSize = (ScreenUtils.getScreenWidth() - pad) / 4;
            //设置item 的大小
            layoutParams = new ViewGroup.LayoutParams(mImageSize, mImageSize);
        }

        @Override
        protected void onBind(MultiItemBean<String> multiItemBean) {
            iv_delete.setVisibility(View.VISIBLE);
            loadItemInfo(iv_pic.getContext(), multiItemBean.getType(), multiItemBean.getData());
            tv_desc.setText(multiItemBean.getExtra());
//            if (multiItemBean.getType() == TaskFileOperationContract.ADD_TYPE) {
//                itemView.setLayoutParams(layoutParams);
//            } else {
//                itemView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
//            }
        }

        private void loadItemInfo(Context context, int type, String url) {
            switch (type) {
                case TaskFileOperationContract.LOCAL_DOC_TYPE:
                case TaskFileOperationContract.NET_DOC_TYPE:
                    //文档
                    String fileExtension = FileUtils.getFileExtension(url);
                    loadDocuIcon(iv_pic, fileExtension);
                    break;
                case TaskFileOperationContract.VIDEO_TYPE://视频
                    GlideUtils.GlideLoadPic(context, R.mipmap.mp4_icon, iv_pic);
                    break;
                case TaskFileOperationContract.ADD_TYPE:
                    GlideUtils.GlideLoadPic(context, R.mipmap.add_image, iv_pic);
                    iv_delete.setVisibility(View.GONE);
                    break;
                default://默认类型图片
                    break;
            }
        }

        //初始化icon
        private void loadDocuIcon(ImageView iv_pic, String fileExtension) {
            //TODO:由于icon缺失，暂时选择word文档的icon
            Context context = iv_pic.getContext();
            if (Constants.EXTENSION_DOC.equals(fileExtension) || Constants.EXTENSION_DOCX.equals(fileExtension)) {
                //doc文档
                GlideUtils.GlideLoadPic(context, R.mipmap.file_icon, iv_pic);
            } else if (Constants.EXTENSION_XLS.equals(fileExtension) || Constants.EXTENSION_XLSX.equals(fileExtension)) {
                //xls文档
                GlideUtils.GlideLoadPic(context, R.mipmap.file_icon, iv_pic);
            } else if (Constants.EXTENSION_PDF.equals(fileExtension)) {
                //pdf文档
                GlideUtils.GlideLoadPic(context, R.mipmap.file_icon, iv_pic);
            } else if (Constants.EXTENSION_PPT.equals(fileExtension) || Constants.EXTENSION_PPTX.equals(fileExtension)) {
                //ppt文档
                GlideUtils.GlideLoadPic(context, R.mipmap.file_icon, iv_pic);
            } else {
                //默认显示文本
            }
            RequestOptions error = RequestOptions.centerInsideTransform() // 居中剪切
                    .placeholder(R.mipmap.icon_img_loading)
                    .error(R.mipmap.file_icon);
            GlideUtils.GlideLoadPic(context, R.mipmap.file_icon, iv_pic,error);
        }
    }


    public static class HeadHolder extends ViewHolder<MultiItemBean<String>> {
        @BindView(R.id.iv_desc_icon)
        TextView iv_desc_icon;

        public HeadHolder(View itemView) {
            super(itemView);
        }

        @Override
        protected void onBind(MultiItemBean<String> bean) {
            iv_desc_icon.setText(bean.getData());
        }
    }

}
